package activity.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.itboye.hutoubenjg.R;

import java.util.Observable;
import java.util.Observer;

import activity.MainOneActivity;
import base.BaseActivity;


public class WelcomeActivity extends BaseActivity implements Observer, OnGestureListener, OnTouchListener {

    private GestureDetector detector;
    private ViewFlipper myViewFlipper, viewflipperbtn;
    private int[] imgs = {R.drawable.welcome1, R.drawable.welcome2, R.drawable.welcome3};

    TextView jump;
    SharedPreferences mySharedPreferences;
    boolean isAbout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_welcome);
        setStatusBarColor(R.color.welcom_statusbar);

        isAbout=getIntent().getBooleanExtra("isAbout",false);


        // 实例化SharedPreferences对象（第一步）
        mySharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE);
        // 实例化SharedPreferences.Editor对象（第二步）

        // 使用getString方法获得value，注意第2个参数是value的默认值
        String firstLogin = mySharedPreferences.getString("firstLogin", "");
        if(isAbout){
            myViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
            myViewFlipper.setOnTouchListener(this);
            jump = (TextView) findViewById(R.id.jump);
            jump.setVisibility(View.GONE);

            viewflipperbtn = (ViewFlipper) findViewById(R.id.viewflipperbtn);
            viewflipperbtn.setOnTouchListener(this);
            myViewFlipper.setLongClickable(true);// 设置长按事件

            // myViewFlipper.setAutoStart(true);// 设置是否自动播放，默认不自动播放
            // myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
            // R.drawable.slide_left_in) );
            // myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.drawable.slide_left_out)
            // );
            detector = new GestureDetector(this);
            addFlipperView();

            return;
        }

        if ("".equals(firstLogin)) {
            myViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
            myViewFlipper.setOnTouchListener(this);
            jump = (TextView) findViewById(R.id.jump);

            viewflipperbtn = (ViewFlipper) findViewById(R.id.viewflipperbtn);
            viewflipperbtn.setOnTouchListener(this);
            myViewFlipper.setLongClickable(true);// 设置长按事件

            // myViewFlipper.setAutoStart(true);// 设置是否自动播放，默认不自动播放
            // myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
            // R.drawable.slide_left_in) );
            // myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.drawable.slide_left_out)
            // );
            detector = new GestureDetector(this);
            addFlipperView();

            jump.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(WelcomeActivity.this, MainOneActivity.class);
                    startActivity(intent);
                    finish();
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    // 用putString的方法保存数据
                    editor.putString("firstLogin", "true");
                    // 提交当前数据
                    editor.commit();

                }
            });
        } else {
            Intent intent = new Intent(WelcomeActivity.this, SplashActivity.class);
            startActivity(intent);
            finish();

        }
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub

    }


    /**
     * 向FlipperView中动态添加View
     */
    private void addFlipperView() {
        for (int i = 0; i < imgs.length; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.myviewflipper, null);

            ImageView iv = (ImageView) view.findViewById(R.id.view_image);

            iv.setBackgroundResource(imgs[i]);
            if(isAbout){

            }else{
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(WelcomeActivity.this, MainOneActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
            }
            myViewFlipper.addView(view);

        }

    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    private void showPreviousView() {

        viewflipperbtn.showPrevious();
        myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
        myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_out));
        myViewFlipper.showPrevious();
    }

    private void showNextView() {
        viewflipperbtn.showNext();
        myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_in));
        myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_out));
        myViewFlipper.showNext();

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return detector.onTouchEvent(event);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // TODO Auto-generated method stub
        if (myViewFlipper.getDisplayedChild() == 0) {
            if (e1.getX() - e2.getX() >= 100) {
                showNextView();
            }
        } else if (myViewFlipper.getDisplayedChild() == 2&&isAbout==false) {

            Intent intent = new Intent(WelcomeActivity.this, MainOneActivity.class);
            startActivity(intent);
            finish();
            SharedPreferences.Editor editor = mySharedPreferences.edit();
            // 用putString的方法保存数据
            editor.putString("firstLogin", "true");
            // 提交当前数据
            editor.commit();
        } else {
            if (e1.getX() - e2.getX() >= 100) {
                showNextView();
            } else if (e2.getX() - e1.getX() >= 100) {
                showPreviousView();
            }
        }

        return false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onInit() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onReadIntent(Bundle bundle) {
        // TODO Auto-generated method stub

    }

}
